<?php
/*
Plugin Name: Download Protect
Plugin URI: http://www.pulsedevelopment.com/development/download-protect/
Description: Protect your downloads from non-members and leechers.
Version: 1.2.0
Author: webprodigy
Author URI: http://www.pulsedevelopment.com
*/

/*  Copyright 2010  PulseDevelopment.com (email : info@pulsedevelopment.com)
    This file is part of Download Protect.

    Download Protect is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Download Protect is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Download Protect.  If not, see <http://www.gnu.org/licenses/>.

*/
require_once('admin/editor.php');
register_activation_hook(__FILE__, "dlprotect_install");
register_deactivation_hook(__FILE__, "dlprotect_uninstall");
//add_filter('the_content', 'dlprotect_ProtectedContent');

// [dlprotect file="file-value"]
function dlprotect_func($atts, $content = null) {
	extract(shortcode_atts(array(
		'file' => '',
		'visibility' => get_option('dlprotect_default_visibility'),
	), $atts));
	if($content !=null) $downloadText = $content;
	else $downloadText = get_option('dlprotect_default_link_text');
	if ((is_user_logged_in()) || ($visibility == 'public')) return '<a href="'.get_option('siteurl').'/wp-content/plugins/download-protect/downloader.php?d='.dlprotect_Encrypt(get_option('dlprotect_download_directory').$atts['file']).'&v='.dlprotect_Encrypt($visibility).'"  class="download-protect-link">'.$downloadText.'</a>';
	else return get_option('dlprotect_replacement_code');
}
add_shortcode('dlprotect', 'dlprotect_func');

function dlprotect_Encrypt($string) {
    $result = '';
    $key = get_option('dlprotect_encrypt_key');
	for($i=1; $i<=strlen($string); $i++){
		$char = substr($string, $i-1, 1);
		$keychar = substr($key, ($i % strlen($key))-1, 1);
		$char = chr(ord($char)+ord($keychar));
		$result.=$char;
	}
	return urlencode($result);
}


function dlprotect_install() {
	if(!get_option('dlprotect_download_directory')) add_option("dlprotect_download_directory", WP_PLUGIN_DIR, '', 'yes');
	if(!get_option('dlprotect_encrypt_key')) add_option("dlprotect_encrypt_key", 'dsdak&sa39gD77', '', 'yes');
	if(!get_option('dlprotect_replacement_code')) add_option("dlprotect_replacement_code", 'Login to view.', '', 'yes');
	if(!get_option('dlprotect_default_visibility')) add_option("dlprotect_default_visibility", 'private', '', 'yes');
	if(!get_option('dlprotect_default_link_text')) add_option("dlprotect_default_link_text", 'Download', '', 'yes');
}

function dlprotect_uninstall() {
	if(get_option('dlprotect_download_directory')) delete_option("dlprotect_download_directory");
	if(get_option('dlprotect_encrypt_key')) delete_option("dlprotect_encrypt_key");
	if(get_option('dlprotect_replacement_code')) delete_option("dlprotect_replacement_code");
	if(get_option('dlprotect_default_visibility')) delete_option("dlprotect_default_visibility");
	if(get_option('dlprotect_default_link_text')) delete_option("dlprotect_default_link_text");
}

// Hook for adding admin menus
add_action('admin_menu', 'dlprotect_add_menu');

$dlprotectEditor = new DLProtectEditor();
add_action( 'admin_init', array( $dlprotectEditor, 'action_admin_init' ) );

// Adding the admin Menu
function dlprotect_add_menu() {
    add_submenu_page('options-general.php', 'Download Protect', 'Download Protect', 10, __FILE__, 'dlprotect_add_adminpage');
}

// action function for adding the administrative page
function dlprotect_add_adminpage() { ?>
<div class="wrap">
<h2>Download Protect</h2>
<p style="clear: both;">To use in your posts, upload the protected file into the directory you choose below. Add to your posts using the following shortcode: 
[dlprotect file="yourfile.mp4" visibility="private"]Link Text[/dlprotect]</p>
<p>The 'visibility' attribute is optional, and so is the Link text. The plugin will work with defaults using [dlprotect file="yourfile.mp4"]. When visibility is 'private', this file will have an encrypted download link for members only, and non-members will see the replacement code you specify below. </p>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>

<table class="form-table">
<tr valign="bottom">
	<td align="right">Protected Download Directory</td>
	<td><input id="dlprotect_download_directory" name="dlprotect_download_directory" type="text" value="<?php echo get_option('dlprotect_download_directory'); ?>" size="60" /></td>
</tr>
<tr valign="bottom">
	<td align="right">Download Encryption Key</td>
	<td><input id="dlprotect_encrypt_key" name="dlprotect_encrypt_key" type="text" value="<?php echo get_option('dlprotect_encrypt_key'); ?>" size="60" /></td>
</tr>
<tr valign="top">
	<td align="right">Replacement Code</td>
	<td><textarea id="dlprotect_replacement_code" name="dlprotect_replacement_code" cols="60" rows="10"><?php echo get_option('dlprotect_replacement_code'); ?></textarea></td>
</tr>
<tr valign="top">
	<td align="right">Default Visibility: </td>
	<td><select id="dlprotect_default_visibility" name="dlprotect_default_visibility">
	<option value="private" <?php if (get_option('dlprotect_default_visibility') == 'private') echo 'selected="selected"'; ?>>Private</option><option value="public" <?php if (get_option('dlprotect_default_visibility') == 'public') echo 'selected="selected"'; ?>>Public</option>
	</select></td>
</tr>
<tr valign="bottom">
	<td align="right">Default Link Text</td>
	<td><input id="dlprotect_default_link_text" name="dlprotect_default_link_text" type="text" value="<?php echo get_option('dlprotect_default_link_text'); ?>" size="60" /></td>
</tr>
</table>

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="dlprotect_replacement_code,dlprotect_download_directory,dlprotect_encrypt_key,dlprotect_default_visibility,dlprotect_default_link_text" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
<div style="float: left;"><h4 style="float: left;margin: 0px; padding: 7px;">Like this plugin? </h4><form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="float: left;"> 
<input type="hidden" name="cmd" value="_s-xclick"> 
<input type="hidden" name="hosted_button_id" value="93YYQRZNQEV7Q"> 
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> 
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"> 
</form> <span style="font-size: 18px; display: block; float: left; margin-top: 8px;"> or <a href="mailto:info@webprodigy.ca">Comment</a></span></div>
</div>
<?php } ?>